/*
 * v1.1 by tonydon site:txdnet.cn
 * netbeansIDE 7.1
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txcms.domain.Links;
import cn.txdnet.txcms.query.LinksQuery;
import cn.txdnet.txcms.service.LinksService;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.WebUtil;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author txdnet
 */
public class LinksAction {

//    建立新链接
    public ModelView make(HttpServletRequest req) throws Exception {
        Links lk = BeanUtil.parseRequestToBean(Links.class, req);
        if (!lk.validate()) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        if (LinksService.make(lk)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

//    删除单个链接
    public ModelView delete_batch(HttpServletRequest req) throws Exception {
        String ids = WebUtil.getArrStr(req, "ids", null);
        if (ids == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int del_num = LinksService.deleteBatch(ids);
        if (del_num > 0) {
            return ModelUtil.getTextModel(del_num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

//    更新链接
    public ModelView update(HttpServletRequest req) throws Exception {
        Links lk = BeanUtil.parseRequestToBean(Links.class, req);
        if (!lk.validate()) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        if (LinksService.update(lk)) {
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新发布状态
     *
     * @param req
     * @return
     */
    public ModelView update_pubstate(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        byte pubstate = WebUtil.getByte(req, "pubstate", -1);
        if (ids == null || pubstate < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = LinksService.updatePubstateBatch(ids, pubstate);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 更新权重
     *
     * @param req
     * @return
     */
    public ModelView update_weight(HttpServletRequest req) {
        int lkid = WebUtil.getInt(req, "lkid", -1);
        int weight = WebUtil.getInt(req, "weight", 0);
        if (lkid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = LinksService.updateWeight(lkid, weight);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 批量更新类目
     *
     * @param req
     * @return
     */
    public ModelView update_catelog(HttpServletRequest req) {
        String ids = WebUtil.getArrStr(req, "ids", null);
        int cid = WebUtil.getInt(req, "cid", -1);
        String ctp = WebUtil.getNoNull(req, "ctp", null);
        if (ids == null || cid < 0 || ctp == null) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        int num = LinksService.updateCatalogBatch(ids, cid, ctp);
        if (num > 0) {
            return ModelUtil.getTextModel(num);
        } else {
            return ModelUtil.getTextModel(Message.MSG_FAIL);
        }
    }

    /**
     * 得到信息权重最大值
     *
     * @return
     */
    public ModelView query_max_weight() {
        return ModelUtil.getTextModel(LinksService.queryMaxWeight());
    }

//     分页查询
    public ModelView list_factor(HttpServletRequest req) throws Exception {
        LinksQuery query = BeanUtil.parseRequestToBean(LinksQuery.class, req);
        return ModelUtil.getJsonModel(LinksService.listByFactor(query));
    }

    public ModelView query_link(HttpServletRequest req) {
        int lkid = WebUtil.getInt(req, "lkid", -1);
        if (lkid < 0) {
            return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
        }
        Links lk = LinksService.query(lkid);
        if (lk == null) {
            return ModelUtil.getTextModel(Message.MSG_VALUE_NULL);
        } else {
            return ModelUtil.getJsonModel(lk);
        }
    }
}
